﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Demo.DataModel;
using Demo.Web.Mvvm;

namespace Demo.WebApp
{
    public partial class CustomerEdit : System.Web.UI.UserControl
    {

        [Inject]
        public ICustomerEdit controller { get; set;}

        protected void Page_Load(object sender, EventArgs e)
        {
            //System.Diagnostics.Debug.Assert(controller != null);
            //controller.OnCustomerChanged += new EventHandler(controller_OnCustomerChanged);
            //Visible = controller.SelectCustomer() != null;
        }

        //protected void Page_Unload(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Debug.Assert(controller != null);
        //    controller.OnCustomerChanged -= new EventHandler(controller_OnCustomerChanged);
        //}

        [EventSubscription]
        void OnCustomerChanged(object sender, EventArgs e)
        {
            try
            {
                if (controller.SelectCustomer() != null)
                {
                    CustomerFormView.DataBind();
                    Visible = true;
                }
                else
                {
                    Visible = false;
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.HandleException(this, ex);
            }            
        }

        protected void CustomerDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = controller;
        }

        protected void CustomerFormView_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case Constants.CommandName_Cancel:
                    controller.Reset();
                    MessageBox.Show(Page, Resources.Messages.Edit_Cancelled);
                    break;
            }
        }

        protected void CustomerFormView_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorHandler.HandleException(this, e.Exception);
                e.ExceptionHandled = true;
            }
            else
            {
                MessageBox.Show(Page, Resources.Messages.Edit_Deleted);
            }
        }

        protected void CustomerFormView_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorHandler.HandleException(this, e.Exception);
                e.ExceptionHandled = true;
            }
            else
            {
                MessageBox.Show(Page, Resources.Messages.Edit_Inserted);
            }
        }

        protected void CustomerFormView_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorHandler.HandleException(this, e.Exception);
                e.ExceptionHandled = true;
            }
            else
            {
                MessageBox.Show(Page, Resources.Messages.Edit_Updated);
            }
        }

        protected void CustomerFormView_DataBound(object sender, EventArgs e)
        {
            //TODO:
            //if (controller.SelectCustomer().??? == null)
            //{
            //    CustomerFormView.ChangeMode(FormViewMode.Insert);
            //}
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            controller.Reset();
        }
    }
}